<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018-11-29
 * Time: 10:17
 */

namespace App\Library\Mall\Shop;


use App\Library\AuthenticatedUser;
use Illuminate\Http\Request;

trait AuthManagers
{
    use AuthenticatedUser;
    /**
     * @param Request $request
     * @return \App\Models\ShopAuth|\Illuminate\Database\Eloquent\Model|\Illuminate\Http\JsonResponse|mixed
     */
    public function store(Request $request)
    {
        $newauth = array_only($request->input('auth'), [
            'shop_owner',
            'id_card_no',
            'id_card_pic_1',
            'id_card_pic_2',
            'id_card_pic_3',
            'license_no',
            'license_pic',
            'other_pic',
        ]);

        if ($newauth['shop_owner'] && $newauth['id_card_no'] && $newauth['id_card_pic_1'] &&
            $newauth['id_card_pic_2'] && $newauth['id_card_pic_3']){

            $newauth['auth_status'] = 0;
            $auth = $this->user()->shop->auth()->firstOrCreate([]);
            $auth->update($newauth);
            return $this->sendAuthSavedResponse($request, $auth);
        }
        return $this->sendAuthSavedFailedResponse($request);
    }

    /**
     * @param Request $request
     * @param $auth
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendAuthSavedResponse(Request $request, $auth)
    {
        return ajaxReturn(['auth'=>$auth]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendAuthSavedFailedResponse(Request $request)
    {
        return ajaxError(201, '');
    }
}
